/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						script_hero_numeric_expression.cpp

	$Header: /heroes4/script_hero_numeric_expression.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "script_hero_numeric_expression.h"

#include "hero.h"
#include "sized_int_types.h"
#include "skill.h"
#include "streambuf_operators.h"
#include "script_targeting.h"

// --------------------------------------------------------------------------
// t_script_hero_experience_level members
// --------------------------------------------------------------------------

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------

t_script_hero_experience_level::t_result t_script_hero_experience_level::evaluate(t_expression_context_hero const& context) const
{
	t_hero const* hero = select_hero_by_target(get_target(), NULL, context.hero, context.army, context.opposing_army);
	return hero ? hero->get_level() : 0;
}

t_script_hero_experience_level::t_result t_script_hero_experience_level::evaluate(t_expression_context_town const& context) const
{
	t_hero const* hero = select_hero_by_target(get_target(), context.garrison, NULL, NULL, context.opposing_army);
	return hero ? hero->get_level() : 0;
}

t_script_hero_experience_level::t_result t_script_hero_experience_level::evaluate(t_expression_context_object const& context) const
{
	assert(false);
	return 0;
}

t_script_hero_experience_level::t_result t_script_hero_experience_level::evaluate(t_expression_context_army const& context) const
{
	t_hero const* hero = select_hero_by_target(get_target(), NULL, NULL, context.army, context.opposing_army);
	return hero ? hero->get_level() : 0;
}

t_script_hero_experience_level::t_result t_script_hero_experience_level::evaluate(t_expression_context_global const& context) const
{
	assert(false);
	return 0;
}


// --------------------------------------------------------------------------
// t_script_hero_skill_mastery members
// --------------------------------------------------------------------------

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_script_hero_skill_mastery::read_from_map(
	std::streambuf &	stream,
	int					format_version )
{
	if ( !t_script_hero_numeric_expression::read_from_map( stream, format_version ) )
		return false;

	m_skill_type = t_skill_type( get< t_uint8 >( stream ) );
	return m_skill_type >= 0 && m_skill_type < k_skill_type_count;
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_script_hero_skill_mastery::read(
	std::streambuf &	stream,
	int					format_version )
{
	if ( !t_script_hero_numeric_expression::read( stream, format_version ) )
		return false;

	m_skill_type = get < t_skill_type >(stream); 
	return m_skill_type >= 0 && m_skill_type < k_skill_type_count;
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_script_hero_skill_mastery::write( std::streambuf& stream ) const
{
	if ( !t_script_hero_numeric_expression::write( stream ) )
		return false;

	put < t_skill_type >(stream, m_skill_type); 
	return true;
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------

t_script_hero_skill_mastery::t_result t_script_hero_skill_mastery::evaluate(t_expression_context_hero const& context) const
{
	t_hero const* hero = select_hero_by_target(get_target(), NULL, context.hero, context.army, context.opposing_army);
	return hero ? hero->get_skill(get_skill_type()) : 0;
}

t_script_hero_skill_mastery::t_result t_script_hero_skill_mastery::evaluate(t_expression_context_town const& context) const
{
	t_hero const* hero = select_hero_by_target(get_target(), context.garrison, NULL, NULL, context.opposing_army);
	return hero ? hero->get_skill(get_skill_type()) : 0;
}

t_script_hero_skill_mastery::t_result t_script_hero_skill_mastery::evaluate(t_expression_context_object const& context) const
{
	assert(false);
	return 0;
}

t_script_hero_skill_mastery::t_result t_script_hero_skill_mastery::evaluate(t_expression_context_army const& context) const
{
	t_hero const* hero = select_hero_by_target(get_target(), NULL, NULL, context.army, context.opposing_army);
	return hero ? hero->get_skill(get_skill_type()) : 0;
}

t_script_hero_skill_mastery::t_result t_script_hero_skill_mastery::evaluate(t_expression_context_global const& context) const
{
	assert(false);
	return 0;
}

